<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>registry.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_registry.js.html">registry.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Global renderer and decorator registry.
<a name="line17"></a> * @author attila@google.com (Attila Bodis)
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.ui.registry&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>/**
<a name="line26"></a> * Given a {@link goog.ui.Component} constructor, returns an instance of its
<a name="line27"></a> * default renderer.  If the default renderer is a singleton, returns the
<a name="line28"></a> * singleton instance; otherwise returns a new instance of the renderer class.
<a name="line29"></a> * @param {Function} componentCtor Component constructor function (for example
<a name="line30"></a> *     {@code goog.ui.Button}).
<a name="line31"></a> * @return {goog.ui.ControlRenderer?} Renderer instance (for example the
<a name="line32"></a> *     singleton instance of {@code goog.ui.ButtonRenderer}), or null if
<a name="line33"></a> *     no default renderer was found.
<a name="line34"></a> */
<a name="line35"></a>goog.ui.registry.getDefaultRenderer = function(componentCtor) {
<a name="line36"></a>  // Locate the default renderer based on the constructor&#39;s unique ID.  If no
<a name="line37"></a>  // renderer is registered for this class, walk up the superClass_ chain.
<a name="line38"></a>  var key;
<a name="line39"></a>  /** @type {Function|undefined} */ var rendererCtor;
<a name="line40"></a>  while (componentCtor) {
<a name="line41"></a>    key = goog.getUid(componentCtor);
<a name="line42"></a>    if ((rendererCtor = goog.ui.registry.defaultRenderers_[key])) {
<a name="line43"></a>      break;
<a name="line44"></a>    }
<a name="line45"></a>    componentCtor = componentCtor.superClass_ ?
<a name="line46"></a>        componentCtor.superClass_.constructor : null;
<a name="line47"></a>  }
<a name="line48"></a>
<a name="line49"></a>  // If the renderer has a static getInstance method, return the singleton
<a name="line50"></a>  // instance; otherwise create and return a new instance.
<a name="line51"></a>  if (rendererCtor) {
<a name="line52"></a>    return goog.isFunction(rendererCtor.getInstance) ?
<a name="line53"></a>        rendererCtor.getInstance() : new rendererCtor();
<a name="line54"></a>  }
<a name="line55"></a>
<a name="line56"></a>  return null;
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Sets the default renderer for the given {@link goog.ui.Component}
<a name="line62"></a> * constructor.
<a name="line63"></a> * @param {Function} componentCtor Component constructor function (for example
<a name="line64"></a> *     {@code goog.ui.Button}).
<a name="line65"></a> * @param {Function} rendererCtor Renderer constructor function (for example
<a name="line66"></a> *     {@code goog.ui.ButtonRenderer}).
<a name="line67"></a> * @throws {Error} If the arguments aren&#39;t functions.
<a name="line68"></a> */
<a name="line69"></a>goog.ui.registry.setDefaultRenderer = function(componentCtor, rendererCtor) {
<a name="line70"></a>  // In this case, explicit validation has negligible overhead (since each
<a name="line71"></a>  // renderer is only registered once), and helps catch subtle bugs.
<a name="line72"></a>  if (!goog.isFunction(componentCtor)) {
<a name="line73"></a>    throw Error(&#39;Invalid component class &#39; + componentCtor);
<a name="line74"></a>  }
<a name="line75"></a>  if (!goog.isFunction(rendererCtor)) {
<a name="line76"></a>    throw Error(&#39;Invalid renderer class &#39; + rendererCtor);
<a name="line77"></a>  }
<a name="line78"></a>
<a name="line79"></a>  // Map the component constructor&#39;s unique ID to the renderer constructor.
<a name="line80"></a>  var key = goog.getUid(componentCtor);
<a name="line81"></a>  goog.ui.registry.defaultRenderers_[key] = rendererCtor;
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Returns the {@link goog.ui.Component} instance created by the decorator
<a name="line87"></a> * factory function registered for the given CSS class name, or null if no
<a name="line88"></a> * decorator factory function was found.
<a name="line89"></a> * @param {string} className CSS class name.
<a name="line90"></a> * @return {goog.ui.Component?} Component instance.
<a name="line91"></a> */
<a name="line92"></a>goog.ui.registry.getDecoratorByClassName = function(className) {
<a name="line93"></a>  return className in goog.ui.registry.decoratorFunctions_ ?
<a name="line94"></a>      goog.ui.registry.decoratorFunctions_[className]() : null;
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Maps a CSS class name to a function that returns a new instance of
<a name="line100"></a> * {@link goog.ui.Component} or a subclass, suitable to decorate an element
<a name="line101"></a> * that has the specified CSS class.
<a name="line102"></a> * @param {string} className CSS class name.
<a name="line103"></a> * @param {Function} decoratorFn No-argument function that returns a new
<a name="line104"></a> *     instance of a {@link goog.ui.Component} to decorate an element.
<a name="line105"></a> * @throws {Error} If the class name or the decorator function is invalid.
<a name="line106"></a> */
<a name="line107"></a>goog.ui.registry.setDecoratorByClassName = function(className, decoratorFn) {
<a name="line108"></a>  // In this case, explicit validation has negligible overhead (since each
<a name="line109"></a>  // decorator  is only registered once), and helps catch subtle bugs.
<a name="line110"></a>  if (!className) {
<a name="line111"></a>    throw Error(&#39;Invalid class name &#39; + className);
<a name="line112"></a>  }
<a name="line113"></a>  if (!goog.isFunction(decoratorFn)) {
<a name="line114"></a>    throw Error(&#39;Invalid decorator function &#39; + decoratorFn);
<a name="line115"></a>  }
<a name="line116"></a>
<a name="line117"></a>  goog.ui.registry.decoratorFunctions_[className] = decoratorFn;
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Returns an instance of {@link goog.ui.Component} or a subclass suitable to
<a name="line123"></a> * decorate the given element, based on its CSS class.
<a name="line124"></a> * @param {Element} element Element to decorate.
<a name="line125"></a> * @return {goog.ui.Component?} Component to decorate the element (null if
<a name="line126"></a> *     none).
<a name="line127"></a> */
<a name="line128"></a>goog.ui.registry.getDecorator = function(element) {
<a name="line129"></a>  var decorator;
<a name="line130"></a>  var classNames = goog.dom.classes.get(element);
<a name="line131"></a>  for (var i = 0, len = classNames.length; i &lt; len; i++) {
<a name="line132"></a>    if ((decorator = goog.ui.registry.getDecoratorByClassName(classNames[i]))) {
<a name="line133"></a>      return decorator;
<a name="line134"></a>    }
<a name="line135"></a>  }
<a name="line136"></a>  return null;
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * Resets the global renderer and decorator registry.
<a name="line142"></a> */
<a name="line143"></a>goog.ui.registry.reset = function() {
<a name="line144"></a>  goog.ui.registry.defaultRenderers_ = {};
<a name="line145"></a>  goog.ui.registry.decoratorFunctions_ = {};
<a name="line146"></a>};
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Map of {@link goog.ui.Component} constructor unique IDs to the constructors
<a name="line151"></a> * of their default {@link goog.ui.Renderer}s.
<a name="line152"></a> * @type {Object}
<a name="line153"></a> * @private
<a name="line154"></a> */
<a name="line155"></a>goog.ui.registry.defaultRenderers_ = {};
<a name="line156"></a>
<a name="line157"></a>
<a name="line158"></a>/**
<a name="line159"></a> * Map of CSS class names to registry factory functions.  The keys are
<a name="line160"></a> * class names.  The values are function objects that return new instances
<a name="line161"></a> * of {@link goog.ui.registry} or one of its subclasses, suitable to
<a name="line162"></a> * decorate elements marked with the corresponding CSS class.  Used by
<a name="line163"></a> * containers while decorating their children.
<a name="line164"></a> * @type {Object}
<a name="line165"></a> * @private
<a name="line166"></a> */
<a name="line167"></a>goog.ui.registry.decoratorFunctions_ = {};
</pre>


</body>
</html>
